package com.aidong.web.controller.card;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.MemberCardDao;
import com.aidong.model.wills.ContractInfo;
import com.aidong.util.ExportXls;
import com.aidong.web.controller.BaseController;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"cardQuery"})
public class MemberCardQueryController extends BaseController {
    @Resource
    MemberCardDao memberCardDao;

    public MemberCardQueryController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return (new ModelAndView("card/query/list")).addObject("shopName", this.getTypeList("shopName")).addObject("cardStatus", this.getTypeList("cardStatus")).addObject("dt1", sdf.format(new Date(System.currentTimeMillis() - 259200000L))).addObject("dt2", sdf.format(new Date()));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "name",required = false) String name, @RequestParam(value = "dateType",required = false) String dateType, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "c.shopCode", shopCode);
        cd.eq("c.status", status);
        if(!this.chkNull(name)) {
            ConditionMap mv = new ConditionMap(1);
            mv.like("c.cardCode", "%" + name + "%");
            mv.like("c.memberCode", "%" + name + "%");
            mv.like("i.chName", "%" + name + "%");
            cd.addSub(mv);
        }

        if("2".equals(dateType)) {
            if(!this.chkNull(dateFrom)) {
                cd.ge("c.makeCardDate", this.parseDate(dateFrom, "yyyy-MM-dd"));
            }

            if(!this.chkNull(dateTo)) {
                cd.le("c.makeCardDate", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
            }
        } else {
            if(!this.chkNull(dateFrom)) {
                cd.ge("k.saleDate", this.parseDate(dateFrom, "yyyy-MM-dd"));
            }

            if(!this.chkNull(dateTo)) {
                cd.le("k.saleDate", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
            }
        }

        this.memberCardDao.queryPageSaleList(pg, cd);
        ModelAndView mv1 = new ModelAndView("card/query/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"expt"})
    public ModelAndView expt(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "name",required = false) String name, @RequestParam(value = "dateType",required = false) String dateType, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, HttpServletResponse response) {
        try {
            Pagination e = new Pagination(1, '\uea60');
            e.setPage(Integer.valueOf(1), Integer.valueOf(1));
            ConditionMap cd = new ConditionMap();
            this.addConditionShopCode(cd, "c.shopCode", shopCode);
            cd.eq("c.status", status);
            if(!this.chkNull(name)) {
                ConditionMap xls = new ConditionMap(1);
                xls.like("c.cardCode", "%" + name + "%");
                xls.like("c.memberCode", "%" + name + "%");
                xls.like("i.chName", "%" + name + "%");
                cd.addSub(xls);
            }

            if("2".equals(dateType)) {
                if(!this.chkNull(dateFrom)) {
                    cd.ge("c.makeCardDate", this.parseDate(dateFrom, "yyyy-MM-dd"));
                }

                if(!this.chkNull(dateTo)) {
                    cd.le("c.makeCardDate", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
                }
            } else {
                if(!this.chkNull(dateFrom)) {
                    cd.ge("k.saleDate", this.parseDate(dateFrom, "yyyy-MM-dd"));
                }

                if(!this.chkNull(dateTo)) {
                    cd.le("k.saleDate", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
                }
            }

            this.memberCardDao.queryPageSaleList(e, cd);
            ExportXls xls1 = new ExportXls(response, "制卡综合查询", "制卡综合查询");
            xls1.addHead(new String[]{"门店编号", "门店名称", "会员卡号", "销售日期", "会籍类型", "有效日期起", "有效日期止", "状态", "制卡日期", "领卡日期", "会员编号", "会员姓名"});
            xls1.setWidth(new int[]{10, 15, 10, 12, 20, 12, 12, 12, 20, 20, 20, 10});
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Iterator i$ = e.getResultList().iterator();

            while(i$.hasNext()) {
                ContractInfo contract = (ContractInfo)i$.next();
                xls1.addRow(new Object[]{contract.getShopCode(), this.cache.getValue("shopName", contract.getShopCode()), contract.getCardCode(), contract.getSaleDate() == null?null:sdf.format(contract.getSaleDate()), this.cache.getValue("cardType", String.valueOf(contract.getCardType())), contract.getBeginTime() == null?null:sdf.format(contract.getBeginTime()), contract.getEndTime() == null?null:sdf.format(contract.getEndTime()), this.nvl(this.cache.getValue("cardStatus", String.valueOf(contract.getStatus()))), contract.getMakeCardDate() == null?null:sdf2.format(contract.getMakeCardDate()), contract.getMemberReceiveDate() == null?null:sdf2.format(contract.getMemberReceiveDate()), contract.getMemberCode(), contract.getMemberName()});
            }

            xls1.close();
            this.addUserLog("导出制卡综合查询", e.getResultList().size() + "条记录");
        } catch (Exception var15) {
            this.log.error("import", var15);
        }

        return null;
    }
}